package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.RoleRightMapper;
import com.indusfo.spc.pojo.RoleRight;
import com.indusfo.spc.service.RoleRightService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色权限表业务层
 * @author 贺闻博
 * 
 *   2018年11月30日
 */
@Service
public class RoleRightServiceImpl implements RoleRightService {
	private  static  final Logger logger = LoggerFactory.getLogger(RoleRightServiceImpl.class);
	@Autowired
	private RoleRightMapper roleRightMapper;
    /**
     * 查询角色权限
     * @see com.indusfo.spc.service.RoleRightService#selectRoleRight(com.indusfo.spc.pojo.RoleRight)
     * @author hewb
     * @date 2018年12月24日,下午6:17:38
     */
	@Override
	public JSONObject selectRoleRight(RoleRight roleRight) {
		List<RoleRight> ListRoleRight = null;
		try {
			Integer pageSize = roleRight.getPagesize();
			Integer pageIndex = roleRight.getPageindex();
			if (pageSize != null && pageIndex != null) {
				roleRight.setIncept(pageSize * (pageIndex - 1));
			}
			Integer lRoleId = roleRight.getlRoleId();
				ListRoleRight = roleRightMapper.selectRoleRight(lRoleId);

			return JSONObject.oK("查询成功", ListRoleRight, ListRoleRight.size());

		} catch (GlobalException e) {
			logger.error(e.getMessage(),e);
			return JSONObject.build(500, e.getMessage());
		}

	}
/**
 * 更新角色权限
 * @see com.indusfo.spc.service.RoleRightService#updateRoleRight(com.indusfo.spc.pojo.RoleRight)
 * @author hewb
 * @date 2018年12月24日,下午6:16:45
 */
	@Override
	public JSONObject updateRoleRight(RoleRight roleRight) {
		Integer num = null;

		try {
				if(roleRight.getlRoleId()==null){
					throw new ModifyFailedException("角色id不能为空");
				}
				if(roleRight.getVcMenuCode()==null){
					throw new ModifyFailedException("菜单权限不能为空");
				}
				if(roleRight.getlFightFlag()==null){
					throw new ModifyFailedException("授权信息不能为空");
				}
				if(roleRight.getlDataState()==null){
					throw new ModifyFailedException("数据状态不能为空");
				}
					num =roleRightMapper.insertSelective(roleRight);
					if(num==0){
						throw new ModifyFailedException("保存数据失败");
					}
					return JSONObject.build(200, "保存数据成功", num);


		  } catch (GlobalException e) {
			  logger.error(e.getMessage(),e);
			  return JSONObject.build(500, e.getMessage());
		 }

	}

	/**
	 * 启用和停用和删除角色权限
	 * @see com.indusfo.spc.service.RoleRightService#deleteRoleRight(Long[], Integer)
	 * @author hewb
	 * @date 2018年12月24日,下午6:16:08
	 */
	@Override
	public String deleteRoleRight(Long[] lRoleRightId, Integer lDataState) {
		try {
			if(lRoleRightId==null){
				throw new ModifyFailedException("角色权限Id不能为空");
			}
			if(lDataState == null && lDataState!=(1|2|3)){
				throw new ModifyFailedException("状态信息不能为空");
			}
			int num = roleRightMapper.deleteRoleRight(lRoleRightId, lDataState);
		      String msg = "";
		      if(num==0){
		    	  switch(lDataState){
			    	  case 1:
			    		   throw new ModifyFailedException("启用失败");
			    	  case 2:
			    		   throw new ModifyFailedException("删除失败");
			    	  case 3:
			    		  throw new ModifyFailedException("停用失败");
		    	  }
		      }else{
		    	   switch(lDataState){
			    	   case 1:msg="启用成功";
			    		     break;
			    	   case 2:msg="删除成功";
		    		     break;
			    	   case 3:msg="停用成功";
		    		     break;
		    	   }
		      }
		      return "删除成功";
		} catch (GlobalException e) {
			logger.error(e.getMessage(),e);
			return e.getMessage();
		}

	}
	/**
	 * 将数据永久删除接口(non-Javadoc)
	 * @see com.indusfo.spc.service.RoleRightService#deleteRight(Integer)
	 * @author hewb
	 * @date 2018年12月26日,上午10:41:22
	 */
	@Override
	public JSONObject deleteRight(@Param("lRoleId") Integer lRoleId) {
		try {
			if(lRoleId==null){
				throw new ModifyFailedException("角色id不能为空");
			}
			roleRightMapper.deleteRoleRightAll(lRoleId);
			return JSONObject.oK();
		} catch (GlobalException e) {
			logger.error(e.getMessage(),e);
			return JSONObject.build(500, e.getMessage());
		}
		
	}


	/**
	 * 根据角色ID删除菜单权限关联信息，逻辑删除
	 *
	 * @author xuz
	 * @date 2019/7/9 5:25 PM
	 * @param lRoleIds, lDataState]
	 * @return void
	 */
	@Override
	public void deleteRoleRightByRoleId(Long[] lRoleIds, Integer lDataState) {
		try {
			if(lRoleIds==null){
				throw new ModifyFailedException("角色Id不能为空");
			}
			if(lDataState == null && lDataState!=(1|2|3)){
				throw new ModifyFailedException("状态信息不能为空");
			}
			int num = roleRightMapper.deleteRoleRightByRoleId(lRoleIds, lDataState);
			String msg = "";
			if(num==0){
				switch(lDataState){
					case 1:
						throw new ModifyFailedException("启用失败");
					case 2:
						throw new ModifyFailedException("删除失败");
					case 3:
						throw new ModifyFailedException("停用失败");
				}
			}else{
				switch(lDataState){
					case 1:msg="启用成功";
						break;
					case 2:msg="删除成功";
						break;
					case 3:msg="停用成功";
						break;
				}
			}
		} catch (GlobalException e) {
			logger.error(e.getMessage(),e);
		}
	}

	//---------------------------------- begin --------------------------------------//

	/**
	 * 根据角色ID查询角色权限集合
	 *
	 * @author xuz
	 * @date 2019/7/15 10:14 AM
	 * @param lRoleId
	 * @return java.util.List<com.indusfo.spc.pojo.RoleRight>
	 */
	@Override
	public List<RoleRight> queryRoleRightByRoleId(Integer lRoleId) {
		if (null == lRoleId) {
			throw new ParamsErrorException("角色ID不能为空");
		}
		List<RoleRight> roleRightList = roleRightMapper.selectRoleRight(lRoleId);
		return roleRightList;
	}

	//---------------------------------- end --------------------------------------//
}
